package com.example.testproject.activity.constraint.kit

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewAnimationUtils
import android.view.animation.OvershootInterpolator
import android.view.animation.RotateAnimation
import androidx.constraintlayout.widget.ConstraintHelper
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.forEach
import kotlin.math.hypot

/**
 *
 * @ClassName: LayoutOverHelp
 * @Author: 史大拿
 * @CreateDate: 8/16/22$ 2:40 PM$
 * TODO
 */
class LayoutOverHelp @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : ConstraintHelper(context, attrs, defStyleAttr) {
    // 布局完成时候回调
    override fun updatePostLayout(container: ConstraintLayout) {
        super.updatePostLayout(container)

        // 遍历所有view
//        container.forEach {
//            val rotate = RotateAnimation(0f, 360f)
//            rotate.duration = 2000
//            rotate.interpolator = OvershootInterpolator(4f)
//            it.startAnimation(rotate)
//        }

        // 找到在xml中 app:constraint_referenced_ids="" 的所有view
        referencedIds.forEach {
            val view = container.findViewById<View>(it)
            // 设置动画
            val rotate = RotateAnimation(0f, 360f)
            rotate.duration = 2000
            rotate.interpolator = OvershootInterpolator(4f)
            view.startAnimation(rotate)
        }
    }

    // 测量完成时候回调
    override fun updatePostMeasure(container: ConstraintLayout?) {
        super.updatePostMeasure(container)
    }

    // 绘制完成时候回调
    override fun updatePreDraw(container: ConstraintLayout?) {
        super.updatePreDraw(container)
    }

    // 约束完成时候回调
    override fun updatePostConstraints(constainer: ConstraintLayout?) {
        super.updatePostConstraints(constainer)
    }
}